<?php

jimport('joomla.application.component.controller');

class bgController extends JController {
    
    public function __construct(){
        parent::__construct();
        
        
        $this->model = $this->getModel();
        
        $this->aut = new bgAuthorize();
    }
    
    public function display($cachable = false, $urlparams = false) {
        parent::display($cachable, $urlparams);
    }
    
    /**
     * Remove image when change file
    **/
    public function removeImage(){
        $image_name = JRequest::getVar('image_name');
        if($image_name != null){
            $path = "uploads/images/com_info_leader/$image_name";
            if(file_exists($path)){
                $del = unlink($path);
            }
        }
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    
    /**
     *  Remove info leader
    **/
    public function remove(){
    	$cid = JRequest::getVar('cid');
        
        /* remove imaage in server */
        foreach($cid as $value){
            //get image name
            $img = $this->getModel()->getImgFromCOCH_CODE($value);
            //Del Image
            if($img != null){
                
                if($img->COCH_IMG != null){
                    $path = "uploads/images/com_info_leader/$img->COCH_IMG";
                    if(file_exists($path)){
                        $del = unlink($path);
                    }
                }
                
            }
        }
        
        if($this->model->remove($cid)){
            $this->setRedirect('index.php?option=com_info_leader');
        }else{
            //JError::raiseError(500, 'Error ! Can\'t delete' );
            $error = "エラー！データが削除されません。";
            JRequest::setVar('task','');
            JFactory::getApplication()->enqueueMessage($error, 'error');
        }
    }
    
   /**
    *  Save info leader
   **/
   public function save()
   {
        $data = &JRequest::getVar('jform');
        //$data['COCH_CODE'] = null;
        //echo $data['COCH_CODE'];return;
             // check addnew or edit
            if(isset($data['COCH_CODE']))
            {
                if($this->getModel()->checkExistID($data['COCH_CODE'])){
                    $addNew = false;
                    if($data['COCH_MAIK']==null)
                    {
                        $data['COCH_MAKF']=0;
                    }
                    elseif($data['COCH_MAIK']!=$_POST['COCH_MAIK_OLD']) {
                       
                           $data['COCH_MAKF']=1;
                    }
                    if($data['COCH_MAIL']==null)
                    {
                        $data['COCH_MAIF']=0;
                    }
                    elseif($data['COCH_MAIL']!=$_POST['COCH_MAIL_OLD']) {                       
                            $data['COCH_MAIF']=1;
                    }
                    if($data['COCH_MAIW']==null)
                    {
                        $data['COCH_MAFW']=0;
                    }
                   elseif($data['COCH_MAIW']!=$_POST['COCH_MAIW_OLD']) {
                        
                        $data['COCH_MAFW']=1;
                    }

                } 
            }
            else
            {
                $addNew = true;
                $data['COCH_CODE'] = HelperBG::getIDAuto('coch','COCH_CODE');
            }
            
            if(empty($data['COCH_OCBA_CODE'])){
                    //$error = "VUI LONG NHAP MA TRUNG TAM - CAU LAC BO TRUC THUOC";
                    $error = "所属センターコードを入力してください。";
                    JRequest::setVar('task','error');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
                }elseif(empty($data['COCH_CD'])){
                    //$error = "VUI LONG NHAP MA LEADER";
                    $error = "指導員コードを入力してください。";
                    JRequest::setVar('task','error');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
                }
                elseif(empty($data['COCH_NAME'])){
                    //$error = "VUI LONG NHAP TEN LEADER";
                    $error = "指導員氏名を入力してください。";
                    JRequest::setVar('task','error');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
                }
                else{   
                /* check COCH_CD exist */
                
                //
//                    if($addNew){
//                        if($this->getModel()->checkExistCOCH_CD($data['COCH_CD'])){
//                            //$error = "MA COCH_CD NAY DA CO ROI";
//                            $error = "指導者システムコードは既存します。";
//                            JRequest::setVar('task','error');
//                            JFactory::getApplication()->enqueueMessage($error, 'error');
//                            parent::display();
//                            return;
//                        }
//                    }
//                //get datetime now
                $now = getdate(); 
                $currentTime = $now["hours"] . ":" . $now["minutes"] . ":" . $now["seconds"]; 
                $currentDate =  $now["year"] . "/" . $now["mon"] . "/" . $now["mday"]; 
                $date = $currentDate." ".$currentTime;
                $data['COCH_DATE'] = $date;
    
                $data['COCH_FLAG']=1;
                            
                //Get image leader
                if($addNew == true){            
                    $data['COCH_IMG'] = $data['COCH_IMG_NEW'];
                }else{                
                    if($data['COCH_IMG_NEW'] != null){
                        $data['COCH_IMG'] = $data['COCH_IMG_NEW'];
                        
                    }else{
                        if(!empty($_POST['jformOCBA_IMG_OLD'])){
                            $data['COCH_IMG'] = $_POST['jformOCBA_IMG_OLD'];
                            
                        }
                    }
                }
    
                 if(isset($_POST['cbCOCH_REGI'])){
                        $data['COCH_REGI'] = 1;
                 }
                //XY LY CAC CHECKBOX LIEN KET DAO TAO 
                $model = $this->getModel();
                $a=$model->getTotalLkdt();
                 $wherestrcb="";
                    for($i=1;$i<=$a;$i++) {
                        if(isset($_POST['chl'.$i])){
                            if($wherestrcb==""){
                                $wherestrcb = $i;
                            }else{
                                $wherestrcb =$wherestrcb.",".$i;
                            }
                        }
                    }
                $data['COCH_LKDT_CODE_LIST'] = $wherestrcb;
                
                //XU LY CAC CHECKBOX HUMAN
                $model = $this->getModel();
                $a=$model->getTotalHuman();
                 $wherestrh="";
                    for($i=1;$i<=$a;$i++) {
                        if(isset($_POST['chh'.$i])){
                            if($wherestrh==""){
                                $wherestrh = $i;
                            }else{
                                $wherestrh =$wherestrh.",".$i;
                            }
                        }
                    }
                $data['COCH_HUMAN_CODE_LIST'] = $wherestrh;
                if($this->model->savetemp($data, $addNew)){                    
                
                    $this->setRedirect('index.php?option=com_info_leader');
               
                }else{
                    $error = "エラー！データが記録されません。";
                    JRequest::setVar('task','');
                    JFactory::getApplication()->enqueueMessage($error, 'error');
                }
            
            }     
        
            parent::display();
    }
    
    public function savetemp(){ 
        
        $data = &JRequest::getVar('jform');
        //echo $data['COCH_CODE'];return;
             // check addnew or edit
             
            if($this->getModel()->checkExistID($data['COCH_CODE'])){
                $addNew = false;
            }else{
                $addNew = true;
                $data['COCH_CODE'] = HelperBG::getIDAuto('coch','COCH_CODE');
            }          
            
            $data['COCH_FLAG']=0;
            
            
            //Get image leader
            if($addNew == true){            
                $data['COCH_IMG'] = $data['COCH_IMG_NEW'];
            }else{                
                if($data['COCH_IMG_NEW'] != null){
                    $data['COCH_IMG'] = $data['COCH_IMG_NEW'];
                    
                }else{
                    if(!empty($_POST['jformOCBA_IMG_OLD'])){
                        $data['COCH_IMG'] = $_POST['jformOCBA_IMG_OLD'];
                        
                    }
                }
            }

             if(isset($_POST['cbCOCH_REGI'])){
                    $data['COCH_REGI'] = 1;
             }
             //XU LY CAC CHECKBOX LIEN KET DAO TAO
              $model = $this->getModel();
            $a=$model->getTotalLkdt();
             $wherestrcb="";
                for($i=1;$i<=$a;$i++) {
                    if(isset($_POST['chl'.$i])){
                        if($wherestrcb==""){
                            $wherestrcb = $i;
                        }else{
                            $wherestrcb =$wherestrcb.",".$i;
                        }
                    }
                }
            $data['COCH_LKDT_CODE_LIST'] = $wherestrcb;
            
             //XU LY CAC CHECKBOX HUMAN
            $model = $this->getModel();
            $a=$model->getTotalHuman();
             $wherestrh="";
                for($i=1;$i<=$a;$i++) {
                    if(isset($_POST['chh'.$i])){
                        if($wherestrh==""){
                            $wherestrh = $i;
                        }else{
                            $wherestrh =$wherestrh.",".$i;
                        }
                    }
                }
            $data['COCH_HUMAN_CODE_LIST'] = $wherestrh;
            
            if($this->model->savetemp($data, $addNew)){
                
            
                $this->setRedirect('index.php?option=com_info_leader');
           
            }else{
                //JError::raiseError(500, 'Error ! Can\'t save' );
                //$error = "Error! Can't save!";
                $error = "エラー！データが記録されません。";
                JRequest::setVar('task','');
                JFactory::getApplication()->enqueueMessage($error, 'error');
            }       
        
            parent::display();
    }
    
  
    
    
    public function selectArea(){
        $area_code = JRequest::getCmd('area_code',0);
        $model = $this->getModel();
        $pref = $model->getAllPref($area_code);
        echo '<select name="pref" id="pref" class="medium">';
        echo '<option value="0" selected="selected">指定しない</option>"';                             
        foreach($pref as $value){                       
            echo '<option value="'.$value->PREF_CODE.'">'.$value->PREF_NAME.'</option>';        
        }                       
        echo '</select>';
        $app = &JFactory::getApplication();
        $app->close();
        exit();
    }
    
    //Search Center
     public function searchCenter(){
        $search['OCBA_NAME'] =  JRequest::getVar('OCBA_NAME');
        $this->search_name = $search['OCBA_NAME'];
        $result = $this->getModel()->getAllListCenter($search);
        
        echo '<div class="the_scroll">
               <table class="adminlist">
        		<thead>
        			<tr>
        				<th class="th20">
        					センターコード	
                        </th>
                        <th class="th40">
                            センター名称
                        </th>
        				<th class="th40">
                            センター名称カナ				
                        </th>
        				
        			</tr>
        		</thead>
            		<tbody>';
                      
                        if($result != null){
                            foreach($result as $k=>$row){
                 echo '            
                		  <tr class="row'.($k%2).'">
                			<td class="center">
                                <a href="javascript:void(0,0);" class="link_town_code">'. $row->OCBA_CODE.'</a>
                            </td>
                			<td class="center">'. $row->OCBA_NAME.'</td>
                			<td class="center">'. $row->OCBA_KANA.'</td>
                		  </tr>';
                        
                            }
                        }else{
                echo '      <tr>
                                <td class="center" colspan="3">データは見つかりませんでした。</td>
                            </tr>';
                      
                        }
                     
            	echo '</tbody>
                   
                	</table>
                </div>';
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    //Clear Search Center
    public function clearSearchCenter(){
        $result = $this->getModel()->getAllListCenter();
        
        echo '<div class="the_scroll">
               <table class="adminlist">
        		<thead>
        			<tr>
        				<th class="th20">
        					センターコード	
                        </th>
                        <th class="th40">
                            センター名称
                        </th>
        				<th class="th40">
                            センター名称カナ				
                        </th>
        				
        			</tr>
        		</thead>
            		<tbody>';
                      
                        if($result != null){
                            foreach($result as $k=>$row){
                 echo '            
                		  <tr class="row'.($k%2).'">
                			<td class="center">
                                <a href="javascript:void(0,0);" class="link_town_code" id="jformTOWN_CODE">'. $row->OCBA_CODE.'</a>
                            </td>
                			<td class="center">'. $row->OCBA_NAME.'</td>
                			<td class="center">'. $row->OCBA_KANA.'</td>
                		  </tr>';
                        
                            }
                        }else{
                echo '      <tr>
                                <td class="center" colspan="3">データは見つかりませんでした。</td>
                            </tr>';
                      
                        }
                     
            	echo '</tbody>
                   
                	</table>
                </div>';
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    
    //EXPORT FILE CSV
    public function export_csv(){
        if(HelperBG::export_csv_infoleader()){
            $this->setRedirect('index.php?option=com_info_leader');
        }
        else{
            $error = "エラー！データが記録されません。";
            JRequest::setVar('task','');
            JFactory::getApplication()->enqueueMessage($error, 'error');
        }
        parent::display();
    }
    
    
}
?>